package TTEST;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

import org.dyno.visual.swing.layouts.Constraints;
import org.dyno.visual.swing.layouts.GroupLayout;
import org.dyno.visual.swing.layouts.Leading;

import Files.File_;

//VS4E -- DO NOT REMOVE THIS LINE!
public class Commands_Labs_ extends JInternalFrame {

	private static final long serialVersionUID = 1L;
	private JLabel 
		jLabel0,jLabel1,jLabel2,jLabel5;
	private JTextField 
		jTextField0,jTextField1,jTextField2,jTextField4;
	private JList jList0;
	private JScrollPane jScrollPane0;
	private JButton jButton0;
	public Commands_Labs_(int type) {
		super((type==1)?"Labs":"Commands",true,true,false,true);
		value=type;
		initComponents(type);
	}
	public Commands_Labs_(){
		initComponents(0);
	}
	private JButton getJButton0() {
		if (jButton0 == null) {
			jButton0 = new JButton();
			jButton0.setText(">");
			jButton0.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					jButton0MouseMouseClicked(event);
				}
			});
		}
		return jButton0;
	}
	private JScrollPane getJScrollPane0(int type) {
		if (jScrollPane0 == null) {
			jScrollPane0 = new JScrollPane();
			jScrollPane0.setViewportView(getJList0(type));
		}
		return jScrollPane0;
	}
	DefaultListModel listModel;
	private void getJScrollPane0(){}
	private void getJList0(){}
	private JList getJList0(int y) {
		if (jList0 == null) {
			jList0 = new JList();
		listModel = new DefaultListModel();
		updateList(y);
		/*
		Map<String,String> c= new File_().readF(y);
		if (!c.isEmpty()){
			listModel.clear();
		}
			
			
		Set<String> test = c.keySet();
		for (Entry<String,String> t : c.entrySet()) {
			listModel.addElement(t.getKey());
		    
		}*/
			jList0.setModel(listModel);
			jList0.addMouseListener(new MouseAdapter() {
				public void mouseClicked(MouseEvent event) {
					jList0MouseMouseClicked(event);
				}
			});
		}
		return jList0;
	}
	private JTextField getJTextField2() {
		if (jTextField2 == null) {
			jTextField2 = new JTextField();
		}
		return jTextField2;
	}
	private JTextField getJTextField1() {
		if (jTextField1 == null) {
			jTextField1 = new JTextField();
		}
		return jTextField1;
	}
	private JTextField getJTextField0() {
		if (jTextField0 == null) {
			jTextField0 = new JTextField();
		}
		return jTextField0;
	}
	private JLabel getJLabel2() {
		if (jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setText("IP address:");
		}
		return jLabel2;
	}

	private JLabel getJLabel1() {
		if (jLabel1 == null) {
			jLabel1 = new JLabel();
			//if(value==1)
			jLabel1.setText((value==1)?"Password:":"Command:");
			//else
				//jLabel1.setText("Command:");
		}
		return jLabel1;
	}

	private JLabel getJLabel0() {
		if (jLabel0 == null) {
			jLabel0 = new JLabel();
			//if(value==1)
			jLabel0.setText((value==1)?"Username:":"Command name:");
			//else
				//jLabel0.setText("Command name:");
		}
		return jLabel0;
	}
	
	private void jList0MouseMouseClicked(MouseEvent event) {
		//DefaultListModel listModel;
		if (event.getSource() instanceof JList){
			if(value==1){
		jTextField4.setText(((JList)event.getSource()).getSelectedValue().toString());
		for (Entry<String, String> entry : File_.common_map_labs.entrySet()) {
			System.out.println(entry.getValue());
            if (entry.getKey().equals(((JList)event.getSource()).getSelectedValue().toString())){
            		String[] a=entry.getValue().split(" ");
            		jTextField0.setText(a[0]);
            		jTextField1.setText(a[1]);
            		jTextField2.setText(a[2]);
            		}
		}}
		else
		{
			jTextField0.setText(((JList)event.getSource()).getSelectedValue().toString());
			for (Entry<String, String> entry : File_.common_map_commands.entrySet()) {
				System.out.println(entry.getValue());
	            if (entry.getKey().equals(((JList)event.getSource()).getSelectedValue().toString())){
	            	jTextField1.setText(entry.getValue());
		}}
		
        }
		}
	}
	private int value;
	
	
	private JTextField getJTextField4() {
		if (jTextField4 == null) {
			jTextField4 = new JTextField();
		}
		return jTextField4;
	}
	public void updateList(int g){
		Map<String,String> c= new File_().readF(g);listModel.clear();
		if (!c.isEmpty()){
			for (Entry<String, String> entry :  c.entrySet()) {
				listModel.addElement(entry.getKey());
	        }
		}
	}
	/**
	 * save new data to file and reload list with updated data
	 * @param event
	 */
	private void jButton0MouseMouseClicked(MouseEvent event) {
		if(value==1)
			new File_().addToMapLab(jTextField4.getText(),jTextField0.getText()+" "+jTextField1.getText()+" "+jTextField2.getText(), value);
		else
			new File_().addToMapLab(jTextField0.getText(), jTextField1.getText(), value);
		updateList(value);
	}
	private JLabel getJLabel5() {
		if (jLabel5 == null) {
			jLabel5 = new JLabel();
			jLabel5.setText("Name:");
		}
		return jLabel5;
	}
	private void initComponents(int a) {
		setClosable(true);
		setIconifiable(true);
		setVisible(true);
		setLayout(new GroupLayout());
		boolean param=(a==1)?true:false;
		add(getJLabel0(), new Constraints(new Leading((param)?29:1, 10, 10), new Leading(20, 10, 10)));//Username
		add(getJLabel1(), new Constraints(new Leading(30, 12, 12), new Leading(48, 12, 12)));//Password
		if(param){
		add(getJLabel2(), new Constraints(new Leading(28, 12, 12), new Leading(76, 12, 12)));//IP address
		}
		add(getJScrollPane0(a), new Constraints(new Leading(285, 235, 10, 10), new Leading(70, 135, 12, 12)));
		add(getJButton0(), new Constraints(new Leading(200, 10, 10), new Leading(70, 12, 12)));
		if(param){
		add(getJLabel5(), new Constraints(new Leading(56, 12, 12), new Leading(108, 12, 12)));//Name
		
		add(getJTextField2(), new Constraints(new Leading(104, 73, 10, 10), new Leading(72, 12, 12)));//-ipaddress
		}
		add(getJTextField1(), new Constraints(new Leading(103, (param)?74:434, 12, 12), new Leading(44, 12, 12)));//-password
		add(getJTextField0(), new Constraints(new Leading(103, (param)?74:200, 12, 12), new Leading(16, 12, 12)));//-username
		if(param){
		add(getJTextField4(), new Constraints(new Leading(104, 85, 12, 12), new Leading(104, 12, 12)));//-name
		}
		setSize(550, 240);
	}

}
